package xyz.thoughtset.viewer.common.exc.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import xyz.thoughtset.viewer.common.envvar.service.EnvVarsService;
import xyz.thoughtset.viewer.common.exc.constant.ExcInfoExConstant;
import xyz.thoughtset.viewer.common.exc.entity.ExcEnvVar;
import xyz.thoughtset.viewer.common.exc.event.ExcMsgEvent;
import xyz.thoughtset.viewer.common.exc.service.ExcInfoService;

@Slf4j
@Component
public class ExcInfoListener  implements ApplicationListener<ExcMsgEvent> {
    @Autowired
    private ExcInfoService excInfoService;
    @Autowired
    private EnvVarsService envVarsService;


    @Async(ExcInfoExConstant.EX_NAME)
    @Override
    public void onApplicationEvent(ExcMsgEvent event) {
        ExcEnvVar envVar = envVarsService.loadSingleData(ExcEnvVar.class);
        if (!envVar.isUseSave()) {return;}
        excInfoService.save(event.getExcInfo());
        if (!envVar.needCheck()) {return;}
    }

}
